/**
 * 
 */
package com.music.service.playlists.dao.impl;

import static com.music.service.playlists.constants.PlaylistSQLQueries.*;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import com.music.service.playlists.beans.Playlist;
import com.music.service.playlists.constants.PlaylistState;
import com.music.service.playlists.dao.PlaylistDAO;
import com.music.service.playlists.mapper.PlaylistMapper;

/**
 * @author asanthan
 * 
 */
public class PlaylistDAOImpl implements PlaylistDAO {
	private JdbcTemplate mMusicsTemplate;

	
	/**
	 * @return the musicsTemplate
	 */
	public JdbcTemplate getMusicsTemplate() {
		return mMusicsTemplate;
	}
	/**
	 * @param pMusicsTemplate the musicsTemplate to set
	 */
	@Required
	public void setMusicsTemplate(JdbcTemplate pMusicsTemplate) {
		mMusicsTemplate = pMusicsTemplate;
	}
	/**
	 * 
	 * @param pMemberId
	 * @param pPlaylistName
	 * @return List<Playlist>
	 */
	@Override
	public boolean addPlaylist(final Long pMemberId, final String pPlaylistName) {
		int nRowsUpdated = getMusicsTemplate().update(INSERT_PLAYLIST,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pMemberId);
						preparedStatement.setString(2, pPlaylistName);
						preparedStatement.setInt(3,PlaylistState.ACTIVE.getState());
					}
				});
		return nRowsUpdated == 1;
	}
	
	
	/**
	 * @param pMemberId
	 * @param pPlaylistId
	 * @return  List<Playlist>
	 */
	@Override
	public boolean deletePlaylist(final Long pMemberId, final Long pPlaylistId) {
		int nRowsUpdated = getMusicsTemplate().update(DELETE_PLAYLIST,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setInt(1,PlaylistState.DELETED.getState());
						preparedStatement.setLong(2, pPlaylistId);
						preparedStatement.setLong(3, pMemberId);
					}
				});
		return nRowsUpdated == 1;
	}
	
	
	/**
	 * @param pMemberId
	 * @return List<Playlist>
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Playlist> viewPlaylists(final Long pMemberId) {
		List playlists = getMusicsTemplate().query(VIEW_PLAYLISTS,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pMemberId);
						preparedStatement.setInt(2, PlaylistState.DELETED.getState());
					}
				}, new PlaylistMapper());
		return playlists;
	}
	
	/**
	 * Return Playlist if exists or else null
	 * 
	 * @param pMemberId
	 * @param pPlaylistId
	 * @return Playlist
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Playlist getPlaylistInfoForMember(final Long pMemberId, final Long pPlaylistId) {
		List<Playlist> playlists = null;
		playlists = getMusicsTemplate().query(GET_PLAYLIST_INFO_FOR_MEMBER,
					new PreparedStatementSetter() {
						public void setValues(PreparedStatement preparedStatement)
								throws SQLException {
							preparedStatement.setLong(1, pMemberId);
							preparedStatement.setLong(2, pPlaylistId);
							preparedStatement.setInt(3, PlaylistState.DELETED.getState());
						}
					}, new PlaylistMapper());
		if(playlists != null && !playlists.isEmpty()){
			//return first entry
			return playlists.get(0);
		}
		//return null.
		return null;
	}
	
	
	/**
	 * @param pMemberId
	 * @param pSize
	 * @return List<Playlist>
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Playlist> getRecentPlaylists(final Long pMemberId,final int pSize) {
		List playlists = getMusicsTemplate().query(VIEW_RECENT_PLAYLISTS,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pMemberId);
						preparedStatement.setInt(2, PlaylistState.DELETED.getState());
						preparedStatement.setInt(3, pSize);
					}
				}, new PlaylistMapper());
		return playlists;
	}
	
	/**
	 * @param pPlaylistId
	 * @param pSongId
	 * @param pSongPosition
	 * @return List<Playlist>
	 */
	@Override
	public boolean addSongToPlaylist(final Long pPlaylistId,final Long pSongId,final int pSongPosition){
		int nRowsUpdated = getMusicsTemplate().update(ADD_SONG_TO_PLAYLIST,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pPlaylistId);
						preparedStatement.setLong(2, pSongId);
						preparedStatement.setInt(3,pSongPosition);
					}
				});
		return nRowsUpdated == 1;
	}
	
	/**
	 * @param pPlaylistId
	 * @param pSongId
	 * @return List<Playlist>
	 */
	@Override
	public boolean deleteSongFromPlaylist(final Long pPlaylistId,final Long pSongId){
		int nRowsUpdated = getMusicsTemplate().update(DELETE_SONG_FROM_PLAYLIST,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pPlaylistId);
						preparedStatement.setLong(2, pSongId);
					}
				});
		return nRowsUpdated == 1;
	}
	
	
}
